#include "block.h"

//bikin blok baru
void createBlock(block_t* block){
	(*block).addr = 0;
	(*block).bytes = (char*) malloc (MAX_ADDR_BLOCK+1);
}

//baca blok sesuai alamat
block_t readBlock(FILE* hdd, unsigned long addrBlock){
	block_t Result;
	char *BlockValue;
	BlockValue = (char*) malloc (4001);
	
	//unsigned long addrBlock_int = stringToUl(addrBlock);
	unsigned long long RealAddr = addrBlock * 4000;
	
	fseek(hdd, RealAddr, SEEK_SET);
	createBlock(&Result);
	Result.addr = addrBlock;
	int i;
	for(i = 0; i < 4000; i++){
		Result.bytes[i] = fgetc(hdd);
	}	
	return Result;
}

//tulis blok ke device
void writeBlock(FILE* hdd, block_t block){
	//unsigned long addrBlock_int = stringToUl(block.addr);
	unsigned long long RealAddr = block.addr * 4000;	
	fseek(hdd, RealAddr, SEEK_SET);
	int i;
	for(i = 0; i < 4000; i++){
		fputc(block.bytes[i], hdd);
	}	
}
